<?php
class LinkAction extends Action {

    public function _initialize() {
        $Config = A('Config');
        $Config->checkLogIn();
        $Config->checkUserLevel(array('1'));
    }
    
    public function index() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Link = M('Link');
            import("ORG.Util.Page");
            $count = $Link->where(array('link_type_id' => $id))->count();
            $Page = new Page($count, 25);
            $show = $Page->show();
            $this->assign('page',$show);
            
            $link = $Link->where(array('link_type_id' => $id))->order(array('list_order' => 'asc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
            
            $this->assign('link', $link);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
    public function add() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $LinkType = M('LinkType');
            $link_type = $LinkType->find($id);
            $this->assign('link_type', $link_type);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
    public function insert() {
        if (isset($_POST['submitted'])) {
            $Link = D('Link');
            if ($link = $Link->create()) {
                if ($Link->add()) {
                    $this->success('成功添加链接！');
                } else {
                    $this->error('添加链接失败！');
                }
            } else {
                $this->error($Link->getError());
            }
        }
    }
    
    public function edit() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Link = M('Link');
            $link = $Link->find($id);
            
            $this->assign('link', $link);
            $this->display();
        } else {
            $this->assign('jumpUrl', __APP__);
            $this->error('非法操作！');
        }
    }
    
    public function update() {
        if (isset($_POST['submitted'])) {
            $Link = D('Link');
            
            if ($Link->create()) {
                if (false !== $Link->save()) {
                    $this->success('成功编辑链接！');
                } else {
                    $this->error('编辑链接失败！');
                }
            } else {
                $this->error($Link->getError());
            }
        }
    }
    
    /* 更新排序 */
    public function updateListOrder() {
        if (isset($_POST['submit'])) {
            $Link = M('Link');
            $list_order = $_POST['list_order'];
            foreach ($list_order as $key => $value) {
                $Link->link_id = $key;
                $Link->list_order = $value;
                $Link->save();
            }
            $this->success('成功更新排序！');
        } else {
            $this->error('非法操作！');
        }
        dump($_POST['list_order']);
    }
    
    public function delete() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $Link = M('Link');
            if ($Link->delete($id)) {
                $this->success('成功删除链接！');
            } else {
                $this->error('删除链接失败！');
            }
        }
    }
    
}
?>